package com.crossriver.crgis.portlet;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.crossriver.crgis.cofo.util.ApplicationActionUtil;
import com.crossriver.crgis.cofo.util.ApplicationCofOConstants;
import com.crossriver.crgis.cofo.util.CofORetrieveUtil;
import com.crossriver.crgis.database.model.Application_C_of_O;
import com.crossriver.crgis.database.service.Application_C_of_OLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class PrintCofO
 */
public class PrintCofO extends MVCPortlet {
	
	// logger
	private static Log log = LogFactory.getLog(PrintCofO.class);
 
	
	/**
	 * This is actually doing nothing now.
	 * 
	 */
	@Override
	public void doView(RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		
		HttpServletRequest request = PortalUtil
		.getHttpServletRequest(renderRequest);

		HttpServletRequest origRequest = PortalUtil
		.getOriginalServletRequest(request);
		
		

		System.out.println(origRequest.getAttribute("applicationId"));
		
		super.doView(renderRequest, renderResponse);

	}
	/**
	 * This method for evoking the printer friendly page.
	 * 
	 * @param actionRequest
	 * @param actionResponse
	 */
	public void printerFriendly(ActionRequest actionRequest,
			ActionResponse actionResponse) {


		Application_C_of_O appCofO = ApplicationActionUtil
				.getApplication_C_of_OFromRequest(actionRequest);
		
		//appCofO.setApplicationId("1ff584d6-e4d8-40f9-8372-76d9a97f2931");

		// this is retrieved for use in later stage
		Application_C_of_O application = null;
		try {
			application = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(appCofO.getApplicationId());


		} catch (PortalException e) {
			if (log.isErrorEnabled()) {
				log.debug("Retrieving Application >>" + application.toString()
						+ " <<< failed");
				log.debug("There is a problem retrieving Application information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		} catch (SystemException e) {
			if (log.isDebugEnabled()) {
				log.debug("Retrieving Application >>" + application.toString()
						+ " <<< failed");
				log.debug("There is a problem retrieving Application information "
						+ e.getMessage());
			}
			// do a page for application error.
			actionRequest.setAttribute("jspPage",
					"/html/application_cofo/ApplicationError.jsp");
			actionResponse.setRenderParameter("jspPage",
					"/html/application_cofo/ApplicationError.jsp");

			SessionErrors.add(actionRequest, "application-error");
			e.printStackTrace();
			return;
		}

		
		// retrieve all the information that has been saved and put them on the
		// request scope.
		// assume that their is failure

		String applicationType = application.getApplicationType();

		// check application
		boolean retreiveInformation = CofORetrieveUtil.extractApplicationInfoForDisplay(
				actionRequest, application);

		// test personal info
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_INDIVIDUAL)) {
			if (retreiveInformation) {
				retreiveInformation = CofORetrieveUtil.extractPersonalInfoForDisplay(
						actionRequest, application);
			} else {
				return;
			}
		}

		// do company information
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_ORGANISATION)) {
			if (retreiveInformation) {
				retreiveInformation = CofORetrieveUtil.extractOrganisationInfoForDisplay(
						actionRequest, application);
			} else {
				return;
			}
		}

		// do company contact
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_ORGANISATION)) {
			if (retreiveInformation) {
				retreiveInformation = CofORetrieveUtil.extractCompanyContactInfoForDisplay(
						actionRequest, application);
			} else {
				return;
			}
		}

		// test contact info
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractContactInfoForDisplay(actionRequest,
					application);
		} else {
			return;
		}

		// application history
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractApplicationHistForDisplay(
					actionRequest, application);
		} else {
			return;
		}

		// test property info
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractPropertyInfoForDisplay(actionRequest,
					application);
		} else {
			return;
		}

		// test supporting docs
		if (retreiveInformation) {
			retreiveInformation = CofORetrieveUtil.extractSupportDocForDisplay(actionRequest,
					application);
		} else {
			return;
		}
		

		log.info("Successfully all the information for display >>> "
				+ application.getApplicationId());

		String applicationCompleteJSP = "";
		if (applicationType
				.equalsIgnoreCase(ApplicationCofOConstants.APPLICATION_TYPE_INDIVIDUAL)) {
			applicationCompleteJSP = "/html/application_cofo/individual/ApplicationCofOIndComplete.jsp";
		} else {
			applicationCompleteJSP = "/html/application_cofo/organisation/ApplicationCofOOrgComplete.jsp";
		}
		
		actionRequest.setAttribute("jspPage", applicationCompleteJSP);
		actionResponse.setRenderParameter("jspPage", applicationCompleteJSP);

		actionRequest.setAttribute("showPrinter", "YES");
		
	}
	
	/**
	 * This method is used to override the addSuccessMessage in the parent
	 * class. This override ensures that success message (that is displayed as
	 * default whenever any request is successfully submitted) is prevented.
	 * 
	 * We like to have control over this and this what this override ensures.
	 * 
	 */
	@Override
	protected void addSuccessMessage(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		if (!addProcessActionSuccessMessage) {
			return;
		}
	}

}
